package com.mlink.vs.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccTaskPool;
import com.mlink.vs.service.PiccTaskPoolService;
import com.mlink.vs.service.PiccTaskStatusService;
import com.mlink.vs.util.StreamUtil;

public class TaskAction {
	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	PiccTaskPoolService piccTaskPoolService;
	
	@Resource
	PiccTaskStatusService piccTaskStatusService;

	//抢单接口
	@Execute(validator = false)
	public String fetchTask(){
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		Map<String, String> stsMap = new HashMap<String, String>();

		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String seatId = (String) mapData.get("seatId");
		String groupName = (String) mapData.get("groupName");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(seatId)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "seatId不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(groupName)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "groupName不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}

			List<String> findMaxFirstNumber = piccTaskPoolService.findMaxFirstNumber();
			if(findMaxFirstNumber.get(0)!=null){
				String valueOf = String.valueOf(0);
				if(findMaxFirstNumber.get(0).equals(valueOf)){
					List<PiccTaskPool> piccTaskPoolList = piccTaskPoolService.findTaskGroup(groupName);
					if(piccTaskPoolList!=null&&piccTaskPoolList.size()>0){
						PiccTaskPool piccTaskPool =null;
						for (int i = 0; i < piccTaskPoolList.size(); i++) {
							piccTaskPool = piccTaskPoolList.get(i);
							piccTaskPool.sertId=seatId;
							int j = piccTaskPoolService.updateSertId(piccTaskPool);
							if(j==1){
								break;
							}
						}
						stsMap.put("sts", "0");
						stsMap.put("TaskId",piccTaskPool.taskId);
						stsMap.put("caseNumber",piccTaskPool.caseNumber);
						stsMap.put("carNumber",piccTaskPool.carNumber);
						rep.getWriter().println(gson.toJson(stsMap));
						rep.getWriter().flush();
					}
				}else{
					List<PiccTaskPool> piccTaskPoolList = piccTaskPoolService.findTaskGroupp(groupName);
					if(piccTaskPoolList!=null&&piccTaskPoolList.size()>0){
						PiccTaskPool piccTaskPool =null;
						for (int i = 0; i < piccTaskPoolList.size(); i++) {
							piccTaskPool = piccTaskPoolList.get(i);
							piccTaskPool.sertId=seatId;
							int j = piccTaskPoolService.updateSertId(piccTaskPool);
							if(j==1){
								break;
							}
						}
						stsMap.put("sts", "0");
						stsMap.put("TaskId",piccTaskPool.taskId);
						stsMap.put("caseNumber",piccTaskPool.caseNumber);
						stsMap.put("carNumber",piccTaskPool.carNumber);
						rep.getWriter().println(gson.toJson(stsMap));
						rep.getWriter().flush();
					}
				}
				
			}else{
				stsMap.put("sts", "1");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}